package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Region;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.query.DestinationQuery;
import cn.wolfcode.luowowo.article.query.DestinationRecommedQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IRegionService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationMapper destinationMapper;

    @Autowired
    private IRegionService regionService;


    @Override
    public List<Destination> getDestsByDeep(int deep) {
        return destinationMapper.selectDestsByDeep(deep);
    }

    @Override
    public void saveOrUpdate(Destination destination) {

    }

    @Override
    public List<Destination> getDestByRegionId(Long rid) {

        //rid ==-1  表示查询国内所有省份

        if(rid  == -1){
            //id = 1; 表示中国id
            return destinationMapper.selectSubDestParentId(1L);
        }



        //区域
        Region region = regionService.get(rid);
        //关联的目的地id
        Long[] destIds = region.getRefIds();

        return destinationMapper.selectDestByIds(destIds);
    }


    @Override
    public PageInfo query(DestinationQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(destinationMapper.selectForList(qo));
    }

    @Override
    public List<Destination> getToasts(Long parentId) {
        //parentId: 广州
        //: 根 >> 中国  >> 广东 >> 广州
        List<Destination> list = new ArrayList<>();

        if (parentId != -1) {
            createToast(parentId, list);
            Collections.reverse(list);  //列表反序
        }
        return list;
    }

    //创建吐司
    private void createToast(Long parentId, List <Destination> list){
        //广州
        Destination destination = destinationMapper.selectByPrimaryKey(parentId);
        if(destination == null){
            return;
        }

        list.add(destination);

        if(destination.getParent() != null){
            createToast(destination.getParent().getId(), list);
        }

    }

    @Override
    public Destination getCountry(Long id) {
        List<Destination> list = this.getToasts(id);

        if(list != null && list.size() > 0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public Destination getProvince(Long id) {

        //必须是国内的才设置省份

        List<Destination> list = this.getToasts(id);
        if(list != null && list.size() > 0){
            //国外
            if(list.get(0).getId() != 1){
                return null;
            }
        }

        if(list.size() > 1){
            return list.get(1);
        }

       /* Destination destination = destinationMapper.selectByPrimaryKey(id);

        //国家
        if(destination.getDeep() == 1){
            return null;
        }

        //省份
        if (destination.getDeep() == 2){
            return destination;
        }


        //城市
        if (destination.getDeep() == 3){
            return destination.getParent();
        }*/


        return null;
    }

    @Override
    public List<Destination> list() {
        return destinationMapper.selectAll();
    }

    //改变热门状态
    public void changeHotValue(Long id, Long hot) {
        destinationMapper.updateHotValue(id, hot);
    }

    //根据ID删除目的地
    public void deleteById(Long id) {
        destinationMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据id查询目的地
     * @param id
     * @return
     */
    @Override
    public Destination getByDestId(Long id) {
        return destinationMapper.selectByPrimaryKey(id);
    }

    @Override
    public String getGeneralizeByDestId(Long id) {
        return destinationMapper.selectGeneralizeByByDestId(id);
    }

    public List<Destination> listByMonthIdAndThenmeId(DestinationRecommedQuery qo) {
        return destinationMapper.listByMonthIdAndThenmeId(qo);
    }

    public List<Destination> getDestsByParentId(Long id) {
        return destinationMapper.selectDestParentId(id);
    }

    public List<Destination> listAll() {

        return destinationMapper.listAll();
    }

    @Override
    public List<Destination> selectByHotAndParentId(Long destId) {

        return destinationMapper.selectByHotAndParentId(destId);
    }

    public List<Destination> listOverseasDest() {
        return destinationMapper.listOverseasDest();
    }

    public List<Destination> listByThemeName(String themeName) {
        return destinationMapper.listByThemeName(themeName);
    }

    public List<Destination> listChinaDest() {
        return destinationMapper.listByChinaId();
    }

}
